//
//  PageDetailViewController.swift
//  ShopIOS
//
//  Created by Apple on 2019/8/20.
//  Copyright © 2019 Apple. All rights reserved.
//

import UIKit

class PageDetailViewController: UIViewController {
    @IBOutlet weak var scrollview: UIScrollView!
    @IBOutlet weak var contentview: UIView!
    @IBOutlet weak var lb_name: UILabel!
    @IBOutlet weak var lb_phone: UILabel!
    @IBOutlet weak var lb_address: UILabel!
    @IBOutlet weak var lb_createtime: UILabel!
    @IBOutlet weak var uv_service: UIView!
    @IBOutlet weak var lb_status: UILabel!
    @IBOutlet weak var lb_ordernum: UILabel!
    @IBOutlet weak var lb_bxlx: UILabel!
    @IBOutlet weak var lb_gdlx: UILabel!
    @IBOutlet weak var lb_hssj: UILabel!
    @IBOutlet weak var lb_yjhssj: UILabel!
    @IBOutlet weak var lb_smsj: UILabel!
    @IBOutlet weak var lb_sfyfpj: UILabel!
    @IBOutlet weak var lb_brand: UILabel!
    @IBOutlet weak var lb_cate: UILabel!
    @IBOutlet weak var lb_type: UILabel!
    @IBOutlet weak var lb_memo: UILabel!
    @IBOutlet weak var lb_ordermoney: UILabel!
    var OrderID:String!
    init(OrderID:String){
        super.init(nibName: nil, bundle: nil)
        self.OrderID=OrderID
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        contentview.snp.remakeConstraints { (make) in
            make.width.equalTo(screenW)
        }
        uv_service.addOnClickListener(target: self, action: #selector(call))
        getOrderDetail()
        // Do any additional setup after loading the view.
    }
    @objc func call(){
        //创建UIAlertController(警告窗口)
        let alert = UIAlertController(title: "提示", message: "是否拨打电话给客服", preferredStyle: .alert)
        //创建UIAlertController(动作表单)
        //        let alertB = UIAlertController(title: "Information", message: "sub title", preferredStyle: .actionSheet)
        //创建UIAlertController的Action
        let OK = UIAlertAction(title: "确定", style: .default) { (UIAlertAction) in
            // phoneStr:  电话号码
            let phone = "tel://" + "18767773654"
            if UIApplication.shared.canOpenURL(URL(string: phone)!) {
                UIApplication.shared.openURL(URL(string: phone)!)
            }
        }
        let Cancel = UIAlertAction(title: "取消", style: .cancel) { (UIAlertAction) in
            print("you selected cancel")
        }
        //将Actiont加入到AlertController
        alert.addAction(OK)
        alert.addAction(Cancel)
        //以模态方式弹出
        self.present(alert, animated: true, completion: nil)
    }
    func getOrderDetail(){
        let d = ["OrderID":OrderID]as[String:String]
        AlamofireHelper.post(url: GetOrderInfo, parameters: d, successHandler: {[weak self](res)in
            HUD.dismiss()
            guard let ss = self else {return}
            let detail=mXgyOrderDetail.init(json: res["Data"])
            ss.lb_name.text=detail.UserName
            ss.lb_phone.text=detail.Phone
            ss.lb_address.text=detail.Address
            ss.lb_createtime.text=detail.CreateDate?.replacingOccurrences(of: "T", with: " ")
            ss.lb_status.text=detail.StateStr
            ss.lb_ordernum.text="\(detail.OrderID)"
            ss.lb_bxlx.text=detail.GuaranteeStr
            ss.lb_gdlx.text=detail.TypeName
            ss.lb_hssj.text="\(detail.RecycleOrderHour)"
            //当前时间的时间戳
            let timeInterval:TimeInterval = getDateFromTime(time: (detail.CreateDate?.replacingOccurrences(of: "T", with: " "))!).timeIntervalSince1970
            let timeStamp = Int(timeInterval)
            print("当前时间的时间戳：\(timeStamp)")
            
            //转换为时间
            let timeIntervalnew:TimeInterval = TimeInterval(timeStamp+detail.RecycleOrderHour*3600)
            let date = Date(timeIntervalSince1970: timeIntervalnew)
            
            //格式话输出
            let dformatter = DateFormatter()
            dformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            print("对应的日期时间：\(dformatter.string(from: date))")
            
            ss.lb_yjhssj.text="注：预计回收时间\(dformatter.string(from: date))"
            if detail.SendOrderList.count>0{
                ss.lb_smsj.text=detail.SendOrderList[0].ServiceDate?.replacingOccurrences(of: "T", with: " ")
            }else{
                ss.lb_smsj.text=""
            }
            ss.lb_sfyfpj.text=detail.AccessorySendStateStr
            ss.lb_brand.text=detail.BrandName
            ss.lb_cate.text=detail.CategoryName
            ss.lb_type.text=detail.SubCategoryName
            ss.lb_memo.text=detail.Memo
            ss.lb_ordermoney.text="￥\(detail.OrderMoney)"
        }){[weak self] (error) in
            HUD.dismiss()
            guard let ss = self else {return}
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
